#!/usr/bin/env python
#coding:utf-8
"""
  Author: u"FQ·BZFS" --<2054489647@qq.com>
  Purpose: u"博发™磁盘TXT阅读器wxPython版"
  Created: 2022/12/16
  Version: 0.1.1
"""
 
import wx
import os
 
 
# class FileDialog(wx.Frame):
class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.my_text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        btn = wx.Button(self, label='Open Text File')
        btn.Bind(wx.EVT_BUTTON, self.onOpen)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.my_text, 1, wx.ALL|wx.EXPAND)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)

        self.SetSizer(sizer)

    def onOpen(self, event):
        wildcard = "TXT files (*.txt)|*.txt"
        dialog = wx.FileDialog(self, "Open Text Files", wildcard=wildcard,
                               style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        if dialog.ShowModal() == wx.ID_CANCEL:
            return

        path = dialog.GetPath()

        if os.path.exists(path):
            with open(path) as fobj:
                for line in fobj:
                    self.my_text.WriteText(line)

"""
    def __init__(self):
        wx.Frame.__init__(self,None,-1,u"文件选择对话框")
        b = wx.Button(self,-1,u"文件选择对话框")
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)
        
    def OnButton(self, event):
        dlg = wx.FileDialog(self,u"选择文件夹",style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            print(dlg.GetPath()) #文件夹路径
            
        dlg.Destroy()

"""

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Text File Reader')

        panel = MyPanel(self)

        self.Show()
 
if __name__ == '__main__':
    frame = wx.PySimpleApp()
    # app = FileDialog()
    app = MyFrame()
    app.Show()
    frame.MainLoop()